﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DAO;
using DTO;

namespace baitaptuan5
{
    public partial class ThemSach : Form
    {
        public ThemSach(SachDTO sach)
        {
            InitializeComponent();

            tgbus = new TacGiaBUS();
            tgdto = new TacGiaDTO();

            tlbus = new TheloaiBUS();
            tldto = new TheLoaiDTO();

            sachbus = new SachBUS();
            sachdto = sach;


            dtSach = sachbus.laybang();
            dtTacGia = tgbus.laybang();
            dtTheLoai = tlbus.laybang();

            if (sach.Masach != 0)
            {
                
                loadCombobox();
                label6.Visible = true;
                numericUpDown2.Visible = true;
                numericUpDown2.ReadOnly = true;
                capnhat();
                action = 0;
            }
            else
            {
                label6.Visible = false;
                numericUpDown2.Visible = false;
                loadCombobox();
                action = 1;
            }

           
        }
        SachBUS sachbus;
        SachDTO sachdto;
        TacGiaBUS tgbus;
        TacGiaDTO tgdto;
        TheloaiBUS tlbus;
        TheLoaiDTO tldto;
        DataTable dtSach, dtTheLoai, dtTacGia;
        int action;
        void capnhat()
        {
            textBox1.Text = sachdto.Tensach;
            numericUpDown1.Text = sachdto.DonGia.ToString();
            numericUpDown2.Text = sachdto.Soluong.ToString();
            comboBox1.SelectedValue = sachdto.Matheloai;
            comboBox2.SelectedValue = sachdto.Matacgia;
        }
        void loadCombobox()
        {
            comboBox1.DataSource = dtTheLoai;
            comboBox1.DisplayMember = "TenTheLoai";
            comboBox1.ValueMember = "MaTheLoai";
            comboBox2.DataSource = dtTacGia;
            comboBox2.DisplayMember = "TenTacGia";
            comboBox2.ValueMember = "MaTG";
            
        }
        private void ThemSach_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
             
                if (textBox1.Text.Trim() == null)
                {
                    MessageBox.Show("CHƯA NHẬP TÊN SÁCH ", "THÔNG BÁO");
                }
                else
                {
                    sachdto.Tensach = textBox1.Text;
                    sachdto.Matacgia = int.Parse(comboBox2.SelectedValue.ToString());
                    sachdto.Matheloai = int.Parse(comboBox1.SelectedValue.ToString());
                    sachdto.DonGia = int.Parse(numericUpDown1.Text);
                    sachdto.Soluong = int.Parse(numericUpDown2.Text);
                    if (action == 1)
                    {
                        sachbus.them(sachdto);
                        MessageBox.Show("THÊM THÀNH CÔNG ", "THÔNG BÁO");
                        Form1 sach = new Form1();
                        this.Dispose();
                    }
                    else
                    {
                        sachbus.sua(sachdto);
                        MessageBox.Show("SỬA THÀNH CÔNG ", "THÔNG BÁO");
                        Form1 sach = new Form1();
                        this.Dispose();
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
